package mysql;

import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.LongValue;
import net.sf.jsqlparser.expression.RowConstructor;
import net.sf.jsqlparser.expression.StringValue;
import net.sf.jsqlparser.expression.operators.relational.ExpressionList;
import net.sf.jsqlparser.expression.operators.relational.ItemsList;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.schema.Column;
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.insert.Insert;
import net.sf.jsqlparser.statement.select.Select;
import net.sf.jsqlparser.statement.select.SelectBody;
import net.sf.jsqlparser.statement.select.SetOperationList;
import net.sf.jsqlparser.statement.values.ValuesStatement;

import java.util.List;
import java.util.stream.Collectors;

public class SqlUtil {

    public static void main(String[] args) {
        sqlToJson();
    }

    /**
     * 解析insert into的sql为json|java对象
     * JSqlParser的使用--sql解析工具 https://blog.csdn.net/qq_39203337/article/details/136946172
     * SQL解释器(JSqlParser)--盘古低代码平台 https://pangea.hisense.com/components/third-party/sql-parser.html
     *
     * @return
     */
    public static void sqlToJson() {
        // String sql = "INSERT INTO users (id, name, age) VALUES (1, 'Alice', 30);";
        String sql = "INSERT INTO Customers (id, CustomerName, ContactName, Address, City, PostalCode, Country, createdTime)\n" +
                "VALUES\n" +
                "(1, 'Cardinal', 'Tom B. Erichsen', 'Skagen 21', 'Stavanger', '4006', 'Norway', '2024-07-02 13:00:01'),\n" +
                "(2, 'Greasy Burger', 'Per Olsen', 'Gateveien 15', 'Sandnes', '4306', 'Norway', '2024-07-02 13:00:01'),\n" +
                "(3, 'Tasty Tee', 'Finn Egan', 'Streetroad 19B', 'Liverpool', 'L1 0AA', 'UK', '2024-07-02 13:00:01');\n";
        try {
            Statement statement = CCJSqlParserUtil.parse(sql);
            if (statement instanceof Insert) {
                Insert insertStatement = (Insert) statement;
                // 表名
                String tableName = insertStatement.getTable().getName();
                System.out.println("Table name: " + tableName);
                // 列
                List<Column> columns = insertStatement.getColumns();
                List<String> columnNames = columns.stream().map(o -> o.getColumnName()).collect(Collectors.toList());
                // 值
                ItemsList values = insertStatement.getItemsList();

                Select select = insertStatement.getSelect();
                SelectBody selectBody = select.getSelectBody();
                if (selectBody instanceof SetOperationList) {
                    SetOperationList operationList = (SetOperationList) selectBody;
                    List<SelectBody> selects = operationList.getSelects();
                    for (SelectBody body : selects) {
                        if (body instanceof ValuesStatement) {
                            ValuesStatement valuesStatement = (ValuesStatement) body;
                            ItemsList itemsList = valuesStatement.getExpressions();
                            if (itemsList instanceof ExpressionList) {
                                ExpressionList expressionList = (ExpressionList) itemsList;
                                List<Expression> expressions = expressionList.getExpressions();
                                for (Expression expression : expressions) {
                                    if (expression instanceof RowConstructor) {
                                        RowConstructor rowConstructor = (RowConstructor) expression;
                                        ExpressionList exprList = rowConstructor.getExprList();
                                        List<Expression> rowConstructorExList = exprList.getExpressions();
                                        // rowConstructorExList.add(new StringValue("0"));
                                        for (Expression expression1 : rowConstructorExList) {
                                            System.out.println(expression1.toString());
                                            if (expression1 instanceof StringValue) {
                                                StringValue value = (StringValue) expression1;
                                                System.out.println(value.getValue());
                                            }
                                            if (expression1 instanceof LongValue) {
                                                LongValue value = (LongValue) expression1;
                                                System.out.println(value.getValue());
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

            }

        } catch (JSQLParserException e) {
            e.printStackTrace();
        }
    }

}
